package liu.zhi.peng;

import javax.swing.*;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class system {
    //添加功能
    public static boolean addStudent(TreeSet<Student> set) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生的信息：姓名,学号,语文，数学，英语");
        //输入学生信息
        String name = scanner.next();
        String id = scanner.next();
        if(!id.matches("[1-9]\\d{8}")){
            System.out.println("\033[31m添加失败，学号输入有误！\033[0m");
            return false;
        }
        double Chinese = scanner.nextDouble();
        double Math = scanner.nextDouble();
        double English = scanner.nextDouble();
        for(Student student : set){
            if(student.id.equals(id)){
                System.out.println("\033[31m系统里面已经有了此学号学生的成绩，不可以重复添加\033[0m");
                return false;
            }
        }
        //添加学生信息
        set.add(new Student(name,  id,  Chinese, Math, English));
        writeStudentsToFile(set);
        System.out.println("\033[31m添加成功！\033[0m");
        return true;
    }

    //删除功能
    public static boolean deleteStudent(TreeSet<Student> set, String id) throws IOException {
        //如果集合中信息是空的话，就不能执行删除操作
        if (set.isEmpty()) {
            System.out.println("\033[31m当前系统没有信息！删除失败\033[0m");
            return false;
        }
        //删除操作
        else {
            Iterator it = set.iterator();
            while (it.hasNext()) {
                Student student = (Student) it.next();
                if (student.id.equals(id)) {
                    it.remove();
                    System.out.println("\033[31m删除成功！\033[0m");
                    writeStudentsToFile(set);
                    return true;
                }
            }
            System.out.println("\033[31m没有找到此学生的信息\033[0m");
            return false;
        }
    }
    //查找功能
    public static void foundStudent(TreeSet<Student> set,String id) {
        //判断是否系统是空
        if(set.isEmpty()) {
            System.out.println("\033[31m系统当前没有信息！\033[0m");
        }
        //如果系统当前有信息
        else {
            Iterator<Student> it = set.iterator();
            while(it.hasNext()){
                Student stu = it.next();
                if(stu.id.equals(id)){
                    System.out.println("查找的结果如下");
                    System.out.println(stu);
                    return ;
                }
            }
            System.out.println("查找结果如下");
            System.out.println("\033[31m空，当前系统不存在该学生\033[0m");
        }
    }
    public static void reviseStudent(TreeSet<Student> set,String id) throws IOException {
        Scanner sc = new Scanner(System.in);
        if(set.isEmpty()) {
            System.out.println("\033[31m不存在此学生\033[0m");
            return ;
        }
        else{
            Iterator<Student> it = set.iterator();
            while(it.hasNext()){
                Student stu = it.next();
                if(stu.id.equals(id)){
                    System.out.println("请重新输入这个学生的基本信息\033[31m（姓名，学号，语文，数学，英语）\033[0m");
                    stu.name = sc.next();
                    stu.id = sc.next();
                    if(!stu.id.matches("[1-9]\\d{8}")){
                        System.out.println("\033[31m修改失败，学号输入有误！\033[0m");
                        return ;
                    }
                    stu.Chinese = sc.nextDouble();
                    stu.Math = sc.nextDouble();
                    stu.English = sc.nextDouble();
                    System.out.println("\033[31m修改成功！\033[0m");
                    writeStudentsToFile(set);
                    return ;
                }
            }
            System.out.println("\033[31m此学生不存在\033[0m");
        }
    }
    public static void lookStudent(TreeSet<Student> set) {
        if(set.isEmpty()) {
            System.out.println("\033[31m当前系统是空\033[0m");
            return ;
        }
        for (Student stu : set) {
            System.out.println(stu);
        }
    }
    public static void averageStudent(TreeSet<Student> set) {
        if(set.isEmpty()) {
            System.out.println("\033[31m当前系统是空\033[0m");
            return;
        }
        double sumChinese = 0;
        double sumMath = 0;
        double sumEnglish = 0;
        for (Student stu : set) {
            sumChinese+=stu.Chinese;
            sumMath+=stu.Math;
            sumEnglish+=stu.English;
        }
        System.out.println("班级的语文平均分是："+sumChinese/set.size());
        System.out.println("班级的数学平均分是："+sumMath/set.size());
        System.out.println("班级的英语平均分是："+sumEnglish/set.size());
    }
    public static String power(){
        System.out.println("\t\t\t\t\t***********老师**************");
        System.out.println("\t\t\t\t\t***********学生**************");
        String power;
        do{
            System.out.println("\033[31m请选择你的身份\033[0m");
            Scanner sc = new Scanner(System.in);
            power = sc.next();
            if(power.equals("老师")||power.equals("学生"))
                return power;
            System.out.println("\033[31m身份选择失败\033[0m");
        }while (!power.equals("老师")&&!power.equals("学生"));
        return power;
    }
    public static void namedStudent(TreeSet<Student> set) {
        if(set.isEmpty()) {
            System.out.println("\033[31m当前系统是空\033[0m");
            return;
        }
        ArrayList<String> list = new ArrayList<>();
        for (Student stu : set) {
            list.add(stu.name);
        }
        String [] arr = list.toArray(new String[list.size()]);
        new GameJFrame(arr,set.size());
    }
    public static void personStudent(TreeSet<Student> set,String id) {
        if(set.isEmpty()) {
            System.out.println("\033[31m当前系统是空\033[0m");
            return ;
        }
        for(Student stu : set){
            if(stu.id.equals(id)){
                System.out.println(stu.name+"的总分是\t"+(stu.English+stu.Math+stu.Chinese));
                System.out.println(stu.name+"的平均分分是\t"+((stu.English+stu.Math+stu.Chinese)/3));
                return ;
            }
        }
        System.out.println("\033[31m系统没有这个人\033[0m");
    }
    public static void qualifiedStudent(TreeSet<Student> set,String subject){
        //单个科目的及格人数
        int qualifiedsum=0;
        if(set.isEmpty()){
            System.out.println("\033[31m当前系统是空\033[0m");
            return;
        }
        if(subject.equals("语文")){
            for(Student student:set){
                if(student.Chinese>=60.0){
                    System.out.println("名字："+student.name+"\t\t"+subject+":\t"+student.Chinese);
                    qualifiedsum++;
                }
            }
            System.out.println("语文的及格人数是："+qualifiedsum);
            return ;
        }else  if(subject.equals("数学")){
            for(Student student:set){
                if(student.Math>=60.0){
                    System.out.println("名字："+student.name+"\t\t"+subject+":\t"+student.Math);
                    qualifiedsum++;
                }
            }
            System.out.println("数学的及格人数是："+qualifiedsum);
            return;
        } else if(subject.equals("英语")) {
            for (Student student : set) {
                if (student.English >= 60.0) {
                    System.out.println("名字："+student.name+"\t\t"+subject+":\t"+student.English);
                    qualifiedsum++;
                }
            }
            System.out.println("英语的及格人数是："+qualifiedsum);
            return ;
        }
        System.out.println("\033[31m很遗憾！班级里面"+subject+"没有及格的！\033[0m");
    }
    public static void writeStudentsToFile(TreeSet<Student> students) throws IOException, IOException {
        FileWriter writer = new FileWriter("D:\\java-code\\student_manage_system2\\src\\liu\\zhi\\peng\\student.txt", false); // 设置为false以覆盖文件内容

        for (Student stu : students) {
            writer.write(stu.toString() + "\n");
        }
        writer.close();
    }
}
